//
//  SQBaseViewController.swift
//  SwiftWeibo
//
//  Created by suqianghotel on 16/11/14.
//  Copyright © 2016年 suqianghotel. All rights reserved.
//

import UIKit

class SQBaseViewController: UIViewController {
    
    
    var tableview: UITableView?
    
    var refreshControl: SQRefreshControl?
    
    var visitorView: SQVisitorView?
    
    var vistiorInfo: NSDictionary?
    
    var isPullup = false
    
    var enablePullup = true
    
    @objc override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor(hex:0xEBEBEB)
        
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccess), name: NSNotification.Name(rawValue: SQUserDidLoginNotification), object: nil)
        
        setupNavigation()
        
        SQHttpManager.shared.logon ? setupTableView() : setupVisitorView()
        
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)
        
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        tableview?.frame = view.bounds
        
    }
    
    
    public func loadData()
    {
        refreshControl?.endRefreshing()
    }
    
    
    
    func setupVisitorView()
    {
        visitorView = SQVisitorView()
        visitorView?.frame = view.bounds
        visitorView?.visitorInfo = vistiorInfo as? [NSString:NSString]
        visitorView?.loginButton.addTarget(self, action: #selector(SQBaseViewController.login), for: .touchUpInside)
        visitorView?.registerButton.addTarget(self, action: #selector(SQBaseViewController.register), for: .touchUpInside)
        visitorView?.attentionButton.addTarget(self, action: #selector(SQBaseViewController.attention), for: .touchUpInside)
        view.addSubview(visitorView!)
        
    }
    
    func setupNavigation()
    {
        
    }
    
    
    private func setupTableView()
    {
        
        tableview = UITableView(frame: view.bounds, style: .plain)
        tableview?.delegate = self
        tableview?.dataSource = self
        tableview?.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 49, right: 0);
        tableview?.scrollIndicatorInsets = (tableview?.contentInset)!
        automaticallyAdjustsScrollViewInsets = false
        view.addSubview(tableview!)
        
        refreshControl = SQRefreshControl()
//        refreshControl?.tintColor = UIColor.orange
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        tableview?.addSubview(refreshControl!)
    
    }
    
    
    
   
    
    
}


extension SQBaseViewController {
    // 注册
    @objc  func register()
    {
        print("register")
    }
    
    @objc  func attention()
    {
        print("attention")
    }
    
    @objc  func login()
    {
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: SQUserShouldLoginNotification), object: nil, userInfo: nil)
        
        print("login")
    }
    
    
    @objc  func loginSuccess()
    {
        self.navigationItem.leftBarButtonItems = nil;
        self.navigationItem.rightBarButtonItems = nil;
        
        NotificationCenter.default.removeObserver(self)
        
        self.view = nil;
        
    }
}


extension SQBaseViewController: UITableViewDelegate, UITableViewDataSource{
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0;
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let section = (tableview!.numberOfSections) - 1
        if section < 0 {
            return
        }
        
        let count = tableview!.numberOfRows(inSection: section) - 1
        
        // 最后一行
        if count  == indexPath.row && section == indexPath.section && isPullup == false && enablePullup == true{
            isPullup = true
            loadData()
        }
        
    }
    
    
    
}
